package com.atguigu.gulimall.order.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 订单
 *
 * @author durunwu
 * @email 172885308@qq.com
 * @date 2023-02-07 14:51:41
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/status/unlock")
    public R<OrderEntity> orderStatus(@RequestParam("orderSn") String orderSn){
        OrderEntity order_sn = orderService.getOne(new QueryWrapper<OrderEntity>().eq("order_sn", orderSn));
        return R.ok().setData(order_sn);
    }

    @GetMapping("/publishMsg")
    public String publishMsg(){
        for (int i = 1; i < 11; i++) {
            if (i % 2 == 0){
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setId(Long.valueOf(i));
                orderEntity.setOrderSn(UUID.randomUUID().toString());
                //convertAndSend(String exchange, String routingKey, Object object)
                rabbitTemplate.convertAndSend("durunwu.direct.exchange","durunwu.RoutingKey",orderEntity);
            }else {
                //1,3,5...
                OrderItemEntity orderItemEntity = new OrderItemEntity();
                orderItemEntity.setId(Long.valueOf(i));
                orderItemEntity.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("durunwu.direct.exchange","durunwu.RoutingKey",orderItemEntity);
            }
        }
        return "ok";
    }

    @GetMapping("/askTest")
    public String askTest(){
        for (int i = 1; i <= 5; i++) {
            OrderItemEntity orderItemEntity = new OrderItemEntity();
            orderItemEntity.setId(Long.valueOf(i));
            orderItemEntity.setOrderSn(UUID.randomUUID().toString());
            rabbitTemplate.convertAndSend("durunwu.direct.exchange","durunwu.RoutingKey",orderItemEntity);
        }
        return "ok";
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
